package com.zhentao.controller;

import com.zhentao.dto.ParamDefinitionDTO;
import com.zhentao.service.ParamDefinitionService;
import com.zhentao.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Tag(name = "参数定义", description = "参数定义管理接口")
@RestController
@RequestMapping("/param/definitions")
@RequiredArgsConstructor
@Slf4j
public class ParamDefinitionController {

    private final ParamDefinitionService paramDefinitionService;

    @Operation(summary = "创建参数定义")
    @PostMapping
    public Result create(@Valid @RequestBody ParamDefinitionDTO dto) {
        boolean ok = paramDefinitionService.createParamDefinition(dto);
        return ok ? Result.OK(true) : Result.ERROR("创建失败");
    }

    @Operation(summary = "检查参数键是否已存在")
    @GetMapping("/check-key")
    public Result checkKey(@RequestParam("paramKey") String paramKey,
                           @RequestParam(value = "excludeId", required = false) Long excludeId) {
        boolean exists = paramDefinitionService.existsByParamKey(paramKey, excludeId);
        return Result.OK(exists);
    }
}


